package com.asiainfo.simpledao;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.sql.DataSource;

public class DaoManager {

	public static Map<String, Dao> daoSet = new ConcurrentHashMap<String, Dao>();

	public static Dao getDao(String daoName) {
		return daoSet.get(daoName);
	}
	
	private DaoManager() {
	}

	public static Dao addDao(String daoName, DatabaseType databaseType, DataSource dataSource) {
		if (daoSet.containsKey(daoName)) {
			throw new RuntimeException("该名称的DAO已存在");
		}
		Dao dao = null;
		if (databaseType == DatabaseType.MYSQL) {
			dao = new MySqlDao(dataSource);
		} else if (databaseType == DatabaseType.ORACLE) {
			dao = new OracleDao(dataSource);
		}
		daoSet.put(daoName, dao);
		return dao;
	}

	public static Dao addDao(String daoName, DatabaseType databaseType, String url, String user, String password) {
		if (daoSet.containsKey(daoName)) {
			throw new RuntimeException("该名称的DAO已存在");
		}
		Dao dao = null;
		if (databaseType == DatabaseType.MYSQL) {
			dao = new MySqlDao(url, user, password);
		} else if (databaseType == DatabaseType.ORACLE) {
			dao = new OracleDao(url, user, password);
		}
		daoSet.put(daoName, dao);
		return dao;
	}

}
